package com.feiyi.app.activity.orderlist;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.feiyi.app.R;
import com.feiyi.app.adapter.MyOrderListAdapter;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseActivity;
import com.feiyi.app.bean.OrderListBean;
import com.feiyi.app.net.HttpMethods;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * @author wangliantao
 * @time 2018 12 2018/12/9 12:41 PM
 * @email 1346947404@qq.com
 * @package com.feiyi.app.activity.orderlist
 * @describe： In me the tiger sniffs the rose
 */
public class MyAwaitReceiveOrderListActivity extends BaseActivity implements OnRefreshListener {

    Unbinder unbinder;

    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.tv_title_titlebar)
    TextView tv_title;

    private String uid;

    private String sid;

    private static final String TAG = "main";

    private List<OrderListBean.DataBean> mList = new ArrayList<>();

    private MyOrderListAdapter mAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
    }
    private void initView() {
        uid = spUtils.getString(AppConstant.UID);
        sid = spUtils.getString(AppConstant.SID);
        tv_title.setText("待收货");
        refreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mAdapter = new MyOrderListAdapter(R.layout.item_order_list,mList);

        mAdapter.setEnableLoadMore(false);
        mAdapter.disableLoadMoreIfNotFullPage(recyclerView);
        recyclerView.setAdapter(mAdapter);

    }
    private void initData() {

        FormBody.Builder builder = new FormBody.Builder();
        if (null != uid && !uid.isEmpty())
            builder.add("uid",uid);
        if (null != sid && !sid.isEmpty())
            builder.add("sid",sid);
        builder.add("page","1");
        builder.add("count","10");
        builder.add("type","shipped");
        RequestBody body = builder.build();

        HttpMethods.getInstance().info.getOrderList(AppConstant.ORDERLIST,body).
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<OrderListBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(OrderListBean orderListBean) {
                        mList.clear();
                        if (null != orderListBean.getData())
                            mList.addAll(orderListBean.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "获取订单列表信息失败 "+e.toString() );
                    }

                    @Override
                    public void onComplete() {
                        Log.e(TAG, "onComplete: "+mList.size() );
                        mAdapter.setNewData(mList);
                        mAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        dispose();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
        refreshLayout.finishRefresh(3000);
    }
}
